<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script language="JavaScript">
function edt(e){
  f.edit.value=e;f.submit()
}
function ende(i){
  f.edit.value='';
  if (i) f.id.value=''
  f.submit()
}
</script>
<noscript></noscript>
<title></title>
</head>
<body><div style="visibility:hidden;position:absolute"><textarea></body></textarea></div>
<?php include("connect.php");
function ipf($n,$l){
  global $$n;	// globale Variable wird hier referenziert: $n: NAME der Variable; $$n INHALT ...
  return "<input name=$n value=\"{$$n}\" ".($l==0?"type=hidden>":"type=input size=$l>") ;
}
function rbp($nr,$v,$nam='srt',$shw=0) {global $_POST;
  return "<input type=radio name='{$nam}[$nr]' value='$v'".($_POST[$nam][$nr]==$v?' checked':'')
		.">".($shw?"($nam,$nr,$v,$_POST[$nam][$nr])":'');
}
function but($n,$v=0){
  return "<input type=button value='$n' onClick='ende($v)'>";
}
function tr($cls){$args=func_get_args();$tmp="<tr $cls>";
  for ($i=1;$i<count($args);$i++) $tmp.= "<td>$args[$i]</td>";
  return $tmp."</tr>\n";
}
function yesorno() {$args=func_get_args();
  global $_POST;
  foreach ($args as $a) $_POST[$a]=($_POST[$a]==''?'n':$_POST[$a]);
}
$felder = array('title','composer','arranger','vocal','comment','mappe');
if (!$_POST[srt]) $_POST[srt][0]='title';
// echo '$_POST-Array:<br>';foreach ($_POST as $k => $val) echo "$k: $val<br>";
// echo "<br>srt-Array:<br>";for ($i=0;$i<count($_POST[srt]);$i++) echo "$i: {$_POST[srt][$i]}<br>";
// START: Daten pflegen ===================================================
if ($_POST[map]) {				 // funktioniert nur, solange noch mindestens 1 Dokument angehakt ist,
   // echo "<br>Mappenbestand: ";  // da $_POST[map] sonst nicht definiert ist!
   $mappe=join(',',$_POST[map]);
   // echo "<br>($mappe)<br>\n";
   // $found = mysql_query("SELECT id,title FROM music WHERE mappe='j' AND id NOT IN ($mappe)");
   // while (list($id,$title) = mysql_fetch_row($found)) echo "$id. $title<br>";
   mysql_query("UPDATE music SET mappe='n' WHERE mappe='j' AND id NOT IN ($mappe)");
   // echo "<br><br>in die Mappe aufzunehmen sind:<br>\n";
   // $found = mysql_query("SELECT id,title FROM music WHERE mappe='n' AND id IN ($mappe)");
   // while (list($id,$title) = mysql_fetch_row($found)) echo "$id. $title<br>";
   mysql_query("UPDATE music SET mappe='j' WHERE mappe='n' AND id IN ($mappe)");
}
if ($_POST[id]>0) {
  yesorno('vocal','mappe');
  $query="UPDATE music SET ";
  foreach ($felder as $v) $query.="$v='{$_POST[$v]}',";
  $query=substr($query,0,-1)." WHERE id=$_POST[id]";
  mysql_query($query);
}
if ($_POST[id]<0 && $_POST[title]!='' ) {
  yesorno('vocal','mappe');
  $query="INSERT INTO music (".join(",",$felder).") VALUES (";
  foreach ($felder as $v) $query.="'$_POST[$v]',";
  $query=substr($query,0,-1).")";
  mysql_query($query);
}
//  echo "<br>Abfrage:<br>$query<br>\n";
// ENDE : Daten pflegen ===================================================
$col =array('light','grey');

echo "<form name=f method=post>"											 // FORMULAR-Start
	."<input type='hidden' name='alle' value='".$_POST[alle]."'> "		   // alle?
	."<input type='hidden' name='edit' value='". ($edit=$_POST[edit]) ."'>"; // zu bearbeitende Zeile (=music.id)
?>
<input type="button" value="Anwesenheit" onClick="ff.action='index.php';ff.submit()">
<input type="button" value="Termine" onClick="ff.action='dates.php';ff.submit()">
<input type="button" value="Mitglieder" onClick="ff.action='members.php';ff.submit()">
<input type="button" value="Probenplan" onClick="ff.action='plan.php';ff.submit()">
<input type="button" value="Noten" onClick="ff.action='noten.php';ff.submit()">
<input type="button" value="Tracks" onClick="ff.action='tracks.php';ff.submit()">
<br><br>
<?php
echo "<h3>$band Repertoire</h3>";
if(!einlog($_POST[password]!=$passwort)){		 // Einlog-Bildschirm, danach erst das folgende ...

													// Tabellenstart
  $breite=array(220,120,120,160);
  echo "<table width=".array_sum($breite)." cellspacing=3 cellpadding=4>\n<tr>";
  foreach ($breite as $w) {echo "<td><img src='img/pix.gif' width=$w height=0></td>";}
  echo "</tr>\n";
													// Verweise zu anderen Seiten ...
  echo "<td colspan=3 class=bot>";
  if (!$_POST[alle]) echo "<input type=button value='neuer Titel' onClick='edt(-1)'> ";
  echo "<input type=button value='".($_POST[alle]?'Mappenauswahl &uuml;bernehmen':'Mappeninhalt pflegen')
	."' onClick='f.alle.value=".(($_POST[alle]+1)%2).";f.submit();'></td>\n";

													// Definition der ****  SORTIERUNG  ****
  $s="<td class=light><table cellpadding=0 cellspacing=0><tr><td><input type=Submit value=sortieren></b>"
	."</td><td>&nbsp;&nbsp;1</td><td>&nbsp;&nbsp;2</td><td>&nbsp;&nbsp;3";
  for ($i=0;$i<4;$i++){$s.="</tr><tr><td>".$felder[$i]."</td>";
	for ($j=0;$j<3;$j++) $s.='<td>'.rbp($j,$felder[$i]).'';
  } echo "$s</tr></table></td>\n";
  $sortstr= " ORDER BY ";
  for ($j=0;$j<3;$j++) $sortstr.= ($_POST[srt][$j]==''?'':"{$_POST[srt][$j]},");$sortstr=substr($sortstr,0,-1);

													// Haupt SELECTION-Aufruf
  $query="SELECT  id,title,composer,arranger,vocal,comment,mappe from music "
		  .($_POST[alle]?'':'WHERE mappe=\'j\'') . $sortstr;
  $fehl = mysql_query($query);


  echo "<tr class={$col[$j++ %2]}><th>Titel</th><th>Komponist</th><th>Arrangeur</th><th>Bemerkungen</th>\n";
  if ($_POST[alle] && !$_POST[edit]) echo "<th>in der<br>Mappe</th>\n";
  echo "</tr>\n";

  $sub="<img src=img/pix.gif width=155 height=0><br><table cellspacing=0 cellpadding=0>";
  if ($_POST[edit]==-1) {$id=-1;
	  echo tr($cls,ipf('id',0).ipf('title',32),ipf('composer',17),ipf('arranger',17),ipf('comment',17),
		 $sub).tr('',cb('vocal')."<br>".cb('mappe'),but('OK').but('cancel',1))."</table>";}
  while (list($id,$title,$composer,$arranger,$vocal,$comment,$mappe) = mysql_fetch_row($fehl)) {
	$cls=" class={$col[$j++ %2]}";
	if ($_POST[edit]==$id) echo tr($cls,ipf('id',0).ipf('title',32),ipf('composer',17),ipf('arranger',17),ipf('comment',17),
		 $sub.tr('',cb('vocal')."<br>".cb('mappe'),but('OK').but('cancel',1))."</table>");
	else {
	 if ($vocal=='j') $title.=" *";
	 if ($_POST[edit]==0) {
	   $title="<a href='#' onClick='edt($id)'>$title</a>";
	   if ($_POST[alle] ) $xtra="<td>". cb('map[]',$id,($mappe=='j'?1:0),0) ."</td>";
	 }
	 echo "<tr $cls><td>$title</td><td>$composer</td><td>$arranger</td><td>$comment</td>$xtra</tr>\n";
	}
  }
  echo "</table>*: mit Gesang<br>";
} // Ende IF-Einlog-Schleife ...
?>
<script language="JavaScript">ff=document.f</script>
</body>
</html>